import { PageResult } from "~/search";
import { Tradable } from "~/extend";

import { ENUM_BOOK_TYPE } from "./core.define";
import { BookDefaultGateway, BookMapGateway } from "./dto.book";
import { Book } from "./schema.book";
import { ExtOwner } from "@/stream-user/core.define";

export const BookRpcFindManyPath = `${BookDefaultGateway.path}/findMany`;
export type findManyBookRpcDto = {
  tradeIDs: Book["tradeId"][];
  /** 凭证号（优先级高）*/
  coupleIDs?: Book["coupleId"][];
};

export const BookRpcInsertManyPath = `${BookDefaultGateway.path}/insertMany`;
export type insertManyBookRpcDto = { schemaList: Book[] };

export const BookRpcDeleteByTradeIDsPath = `${BookDefaultGateway.path}/trade/delete`;
export type deleteBookByTradeIDsRpcDto = { tradeIDs: Book["tradeId"][] };

export const BookRpcDeleteByCoupleIDsPath = `${BookDefaultGateway.path}/couple/delete`;
export type deleteBookByCoupleIDsRpcDto = { coupleIDs: Book["coupleId"][] };

export interface BookRpcInterfaceWithDerive<T> {
  /** 根据交易号或凭证号找出明细账 */
  findMany(dto: T & findManyBookRpcDto): Promise<PageResult<Book>>;

  /** 批量添加明细账 */
  insertMany(dto: T & insertManyBookRpcDto): Promise<Book[]>;

  /** 根据交易号找出明细账，然后删除它们及其结对的明细账 */
  deleteByTradeIDs(dto: T & deleteBookByTradeIDsRpcDto): Promise<PageResult<Book>>;

  /** 根据凭证号找出明细账，然后删除这些结对的明细账 */
  deleteByCoupleIDs(dto: T & deleteBookByCoupleIDsRpcDto): Promise<PageResult<Book>>;
}

export const BookMapRpcSyncQueuePath = `${BookMapGateway.path}/doBusyCheck`;
export type doBusyCheckRpcDto = { maxTask: number };

export const BookMapRpcPutPath = `${BookMapGateway.path}/putBookMap`;
export type putBookMapRpcDto = { types: ENUM_BOOK_TYPE[]; tradableList: (ExtOwner & Tradable)[] };

export interface BookSummaryRpcInterfaceWithDerive<T> {
  doBusyCheck(dto: T & doBusyCheckRpcDto): Promise<null>;
  putBookMap(dto: T & putBookMapRpcDto): Promise<null>;
}
